/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Sbgs;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.service.GeneralQueryService;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.SbgsService;
import me.zhengjie.business.service.dto.GeneralQueryCriteria;
import me.zhengjie.business.service.dto.SbgsQueryCriteria;
import me.zhengjie.business.service.dto.ZhsqQueryCriteria;
import me.zhengjie.pay.domain.PaidQueryCriteria;
import me.zhengjie.pay.service.UnPaidService;
import me.zhengjie.utils.DefaultUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-04-13
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "商保公司管理")
@RequestMapping("/api/sbgs")
public class SbgsController {

    private final SbgsService sbgsService;
    private final GeneralQueryService generalQueryService;

    private final GsglService gsglService;
    private final UnPaidService unPaidService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sbgs:list')")
    public void exportSbgs(HttpServletResponse response, SbgsQueryCriteria criteria) throws IOException {
        sbgsService.download(sbgsService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询商保公司")
    @PreAuthorize("@el.check('sbgs:list','hljg:list')")
    public ResponseEntity<Object> querySbgs(SbgsQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(sbgsService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增商保公司")
    @ApiOperation("新增商保公司")
    @PreAuthorize("@el.check('sbgs:add')")
    public ResponseEntity<Object> createSbgs(@Validated @RequestBody Sbgs resources){
        return new ResponseEntity<>(sbgsService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改商保公司")
    @ApiOperation("修改商保公司")
    @PreAuthorize("@el.check('sbgs:edit')")
    public ResponseEntity<Object> updateSbgs(@Validated @RequestBody Sbgs resources){
        sbgsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除商保公司")
    @ApiOperation("删除商保公司")
    @PreAuthorize("@el.check('sbgs:del')")
    public ResponseEntity<Object> deleteSbgs(@RequestBody String[] ids) {
        sbgsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("拨付单")
    @GetMapping(value = "/queryKb03")
    public ResponseEntity<Object> queryKb03(PaidQueryCriteria criteria, Pageable pageable) {
        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        criteria.setInsuNo(gsgl.getBisGsglYbqh());
        return new ResponseEntity<>(unPaidService.queryKb03(criteria, pageable), HttpStatus.OK);
    }

    @Log("导出拨付单")
    @ApiOperation("导出拨付单")
    @GetMapping(value = "/queryKb03/download")
    public void exportKb03(HttpServletResponse response, PaidQueryCriteria criteria) throws IOException {
        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        criteria.setInsuNo(gsgl.getBisGsglYbqh());
        sbgsService.downloadKb03(sbgsService.queryKb03(criteria), response);
    }

    @ApiOperation("拨付单明细")
    @GetMapping(value = "/queryKb22")
    public ResponseEntity<Object> queryKb22(PaidQueryCriteria criteria, Pageable pageable) {
        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        criteria.setInsuNo(gsgl.getBisGsglYbqh());
        return new ResponseEntity<>(unPaidService.queryKb22(criteria, pageable), HttpStatus.OK);
    }

    @Log("拨付单明细")
    @ApiOperation("拨付单明细")
    @GetMapping(value = "/queryKb22/download")
    public void exportKb22(HttpServletResponse response, PaidQueryCriteria criteria) throws IOException {
        sbgsService.downloadKb22(sbgsService.queryKb22(criteria), response);
    }

    @ApiOperation("查询机构已复核照护申请数量")
    @GetMapping(value = "/org/completed")
    public ResponseEntity<Object> orgCompleted(SbgsQueryCriteria criteria, Pageable pageable) throws IOException {
        String zhlb = "01";
        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        String sbglId = gsgl.getBisGsglId();
        List<String> list = new ArrayList<>();
        list.add("05");
        list.add("06");
        list.add("07");
        list.add("12");
        list.add("13");
        list.add("90");
        return new ResponseEntity<>(sbgsService.queryHljgCntVoPageList(zhlb, sbglId, criteria.getJgxx(), list, pageable), HttpStatus.OK);
    }

    @ApiOperation("查询机构已复核照护申请列表")
    @GetMapping(value = "/org/completedList")
    public ResponseEntity<Object> orgCompletedList(GeneralQueryCriteria criteria,
                                                        @RequestParam(defaultValue = "0")Integer page,
                                                        @RequestParam(defaultValue = "10")Integer size) {
        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        criteria.setZhlb("01");
        criteria.setSbgsId(gsgl.getBisGsglId());
        List<String> list = new ArrayList<>();
        list.add("05");
        list.add("06");
        list.add("07");
        list.add("12");
        list.add("13");
        list.add("90");
        criteria.setZtList(list);
        criteria.setInZt("true");

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryCommonZhxx(criteria, startEnd), HttpStatus.OK);
    }
}